package de.kardass.jsmatep.in.validation;

import java.util.ArrayList;
import java.util.List;

import de.kardass.jsmatep.in.ImportProcessor;
import de.kardass.jsmatep.in.RecordMappedEvent;
import de.kardass.jsmatep.in.RecordMapperListener;
import de.kardass.jsmatep.schema.jSmaTePImportConfig.JSmaTePImportConfigDocument;
import de.kardass.jsmatep.schema.jSmaTePImportConfig.RecordImportConfigType;
import de.kardass.jsmatep.util.ContentReader;

/**
 * Validates a given import file vs. a configuration. (Just a helper tool.)
 *
 * @author Manfred Kardass (manfred@kardass.de)
 *
 */
public class RecordValidationTool implements RecordMapperListener<Object> {
	
	public void validateRecords(String config, String importFile) throws Exception {
		System.out.println("Starting import file validation of file: " + importFile);
		System.out.println("Configuration: " + config);
		final JSmaTePImportConfigDocument theDoc = JSmaTePImportConfigDocument.Factory.parse(config);
        final RecordImportConfigType importConfig = theDoc.getJSmaTePImportConfig();
		final List<String> content = ContentReader.readContents(importFile);
		long recordCounter = 0;
		final List<String> errorRecords = new ArrayList<String>();
		final AbstractRecordValidator validator = new FixedLengthRecordValidator(importConfig);
		for (String aRecord : content) {
			if (!ImportProcessor.skipContent(aRecord)) {
				++recordCounter;
				try {
					validator.validateRecordVsConfig(aRecord);
				} catch (IllegalRecordLengthException e) {
					errorRecords.add(aRecord);
				}
			}
		}
		System.out.println("Validation finished successfully - validated records: " + recordCounter);
		System.out.println("Quantity invalid records: " + errorRecords.size());
		if (errorRecords.size() > 0) {
			System.err.println("Invalid records: ");
			for (String aErrorRecord : errorRecords) {
				System.err.println(aErrorRecord);
			}
		}
	}
	
	/**
	 * Tries to map all records in the import file to a value object.
	 * 
	 * @param config
	 * @param importFile
	 */
	public void tryMappingAgainstValueObject(String config, String importFile) {
		ImportProcessor.importFile(config, this, importFile);
	}
	
	/* (non-Javadoc)
	 * @see de.kardass.jsmatep.in.RecordMapperListener#afterRecordMapped(de.kardass.jsmatep.in.RecordMappedEvent)
	 */
	public void afterRecordMapped(RecordMappedEvent<Object> e) {
		System.out.println("Mapped value object: " + e.getMappedValueObject());
	}

	/**
	 * Parameters: 
	 * 1. xml configuration,
	 * 2. import file
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 2) {
			System.err.println("Config file and import file name expected => operation cancelled!");
			System.exit(-1);
		}
		final String config = args[0];
		final String importFile = args[1];

		try {
			final RecordValidationTool validator = new RecordValidationTool();
			validator.validateRecords(config, importFile);
			validator.tryMappingAgainstValueObject(config, importFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
